package com.wechat.weibo.controller;

import com.wechat.weibo.entity.User;
import com.wechat.weibo.service.FollowService;
import com.wechat.weibo.service.LoginService;
import com.wechat.weibo.util.ResultMap;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Api
@Controller
public class FollowController {
    @Autowired
    LoginService loginService;
    @Autowired
    FollowService followService;
//    关注
    @ApiOperation(value = "关注某人")
    @GetMapping("/follow")
    @ResponseBody
    public ResultMap follow(String selfID, String otherID){
        if(selfID.equals(otherID))return new ResultMap(102,"notYourself",null);
        User self=loginService.getUser(selfID);
        User other=loginService.getUser(otherID);
        if(self==null||other==null)
            return ResultMap.wrong();
        int tmp=followService.follow(self,other);
        if(tmp==0)return ResultMap.success(null);
        else if (tmp==1)return new ResultMap(101,"reFollow",null);
        else return ResultMap.fail(null);
    }
//    取关
    @ApiOperation(value = "取关某人")
    @GetMapping("/cancel")
    @ResponseBody
    public ResultMap cancelFollow(String selfID, String otherID){
        if(selfID.equals(otherID))return new ResultMap(102,"notYourself",null);
        User self=loginService.getUser(selfID);
        User other=loginService.getUser(otherID);
        if(self==null||other==null)
            return ResultMap.wrong();
        int tmp=followService.cancelFollow(self,other);
        if(tmp==0)return ResultMap.success(null);
        else if (tmp==1)return new ResultMap(101,"unFollow already",null);
        else return ResultMap.fail(null);
    }
}
